import json
import os
import platform
import pytest
import conftest
import xml.etree.ElementTree as ET

def set_report_env_on_results():
    """
    在allure-results报告的目录下生成一个写入了环境信息的文件：environment.properties(注意：不能放置中文，否则会出现乱码)
    @return:
    """
    # 需要写入的环境信息
    allure_env = {
        'OperatingEnvironment': "test",
        'BaseUrl': "www.myego.com",
        'PythonVersion': platform.python_version(), # python版本号：3.8.10
        'AllureVersion': '2.18.1',
        'PytestVersion': pytest.__version__,  #pytest版本号7.4.2
    }
    allure_env_file = os.path.join(conftest.BASE_DIR+"/report", 'environment.properties')
    with open(allure_env_file, 'w', encoding='utf-8') as f:
        for _k, _v in allure_env.items():
            f.write(f'{_k}={_v}\n')



def set_report_executer_on_results():
    """
    在./report报告的目录下生成一个写入了执行人的文件：executor.json
    """
    # 需要写入的环境信息
    allure_executor = {
        "name": "admin",   #用户名
        "type": "jenkins",  #运行器用类型
        "buildName": "pytest_ego_demo#1",   #jenkins构建名称
        "buildUrl": "http://192.168.74.133:3000/"  #jenkins地址
        }
    allure_env_file = os.path.join(conftest.BASE_DIR+"/report", 'executor.json')
    with open(allure_env_file, 'w', encoding='utf-8') as f:
        f.write(str(json.dumps(allure_executor, ensure_ascii=False, indent=4)))


def set_report_xml_on_results():
    # 创建一个根元素
    root = ET.Element("environment")

    # 创建子元素并添加数据
    parameters = [
        {"key": "浏览器", "value": "谷歌"},
        {"key": "浏览器版本", "value": "89.0.4389.128"},
        {"key": "项目环境", "value": "测试环境"},
        {"key": "项目地址", "value": "127.0.0.1/login"},
        {"key": "测试人员", "value": "吴彦祖"},
    ]
    for param in parameters:
        parameter = ET.SubElement(root, "parameter")
        ET.SubElement(parameter, "key").text = param["key"]
        ET.SubElement(parameter, "value").text = param["value"]

    # 生成XML树对象
    tree = ET.ElementTree(root)

    # 创建XML文档并写入文件
    tree = ET.ElementTree(root)
    allure_xml_file = os.path.join(conftest.BASE_DIR+"/report", 'environment.xml')
    tree.write(allure_xml_file, encoding="utf-8", xml_declaration=True)


def set_report_xml_on_jenkins():
    # 创建一个根元素
    root = ET.Element("environment")

    # 创建子元素并添加数据
    parameters = [
        {"key": "浏览器", "value": "谷歌"},
        {"key": "浏览器版本", "value": "89.0.4389.128"},
        {"key": "项目环境", "value": "测试环境"},
        {"key": "项目地址", "value": "127.0.0.1/login"},
        {"key": "测试人员", "value": "吴彦祖2"},
    ]
    for param in parameters:
        parameter = ET.SubElement(root, "parameter")
        ET.SubElement(parameter, "key").text = param["key"]
        ET.SubElement(parameter, "value").text = param["value"]

    # 生成XML树对象
    tree = ET.ElementTree(root)

    # 创建XML文档并写入文件
    tree = ET.ElementTree(root)
    allure_xml_file = os.path.join(r"C:\Users\12821\.jenkins\workspace\Gitee_api_demo\allure_jsonfile", 'environment.xml')
    tree.write(allure_xml_file, encoding="utf-8", xml_declaration=True)

# set_report_executer_on_results()
# set_report_env_on_results()

#set_report_xml_on_results()